﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DECOR.DL.DALC.SAPC.Proxy;
using CROSLAND.BL.BE;
using System.Data;

namespace CROSLAND.DL.DALC.SAPC
{
    public class LoteDALC : ILoteDALC
    {
       public static LoteDALC Instance() {
            if (_instance == null)
            {
                lock (typeof(LoteDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new LoteDALC();
                    }
                }
            }
            return _instance;
        }

       private LoteDALC() { }
       private static volatile LoteDALC _instance = null;      
   
        #region ILoteDALC Members

        public List<GenericBE> listar(string pCodMaterial, string pCodCentro, string pCodAlmacen, decimal pCant, string pNumPedido, out string pCodLoteOptimo)
        {
            LotesProxy proxy = new LotesProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            ZWEB_LOTESTable tblResult = new ZWEB_LOTESTable();

            proxy.Zweb_Lista_Lotes(pCant, pCodAlmacen, pCodMaterial, pNumPedido, pCodCentro, out pCodLoteOptimo, ref tblResult);

            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();

            List<GenericBE> listaResult = new List<GenericBE>();
            foreach (DataRow dr in dt.Rows)
            {
                GenericBE obj = new GenericBE();
                obj.Codigo = dr["charg"].ToString();
                //obj.Descripcion = dr["charg"].ToString();
                obj.Descripcion = dr["charg"].ToString() + (dr["calidad"].ToString() != string.Empty ? " - " + dr["calidad"].ToString() : "");
                listaResult.Add(obj);
            }
            return listaResult;
        }

        #endregion
    }
}
